//用来做数据联动 点对点的数据联动
import func from '../../func/socket.js';
import Vue from 'vue';
const USERCLIENT = {
    //用户登录接口
    USER_LOGIN: 'USER_LOGIN',
    //所有用户与其好友的房间接口
    MASSAGE_LIST: 'MASSAGE_LIST',
    //用户与单个好友的信息内容
    USER_MSG_LIST: 'USER_MSG_LIST',
    //设置当期显示的是那个好友的信息列表
    SET_USER_MSG_LIST: 'SET_USER_MSG_LIST'
};
const state = {
    //用户信息
    userInfo: {
        name: ''
    },
    //用户好友列表
    userFriendList: [],
    //用户好友信息存储房间列表
    massageList: {},
    //点对点 如A用户与B用户之间的信息列表
    userMsgList: {}
};
const getters = {
    getUserInfo: state => state.userInfo,
    getUserFriendList: state => state.userFriendList,
    getMassageList: state => state.massageList,
    getUserMsgList: state => state.userMsgList
};
const actions = {
    //登录
    userLogin({ commit, state }, data) {
        let userMsg = {
            userInfo: {
                name: data.userName
            },
            userFriendList: []
        };
        if (data.userName === '11') {
            userMsg.userFriendList = ['22', '33', '44'];
        } else {
            userMsg.userFriendList = ['11', '33', '44'];
        }
        commit(USERCLIENT.USER_LOGIN, userMsg);
        //给用户的所有好友添加连接
        func.userOnline(userMsg, function(data) {
            commit(USERCLIENT.MASSAGE_LIST, data.userFriendList);
        });
    },
    //接收好友发来的信息
    userMsgList({ commit }, data) {
        commit(USERCLIENT.USER_MSG_LIST, data);
    },
    //设置现在渲染的是那个好友的信息内容
    setUserMsgList({ commit }, data) {
        commit(USERCLIENT.SET_USER_MSG_LIST, data);
    },
    /**
     * 发送信息
     * uid 发送人当前id
     * fid 接收人id
     * msg 信息内容
     */
    setUserFriendContent({ commit }, data) {
        func.userFriendMsg(data.uid, data.fid, data.msg);
    }
};
const mutations = {
    [USERCLIENT.USER_LOGIN](state, data) {
        state.userInfo = data.userInfo;
        state.userFriendList = data.userFriendList;
    },
    [USERCLIENT.MASSAGE_LIST](state, data) {
        Object.values(data).forEach(fid => {
            Vue.set(state.massageList, fid, []);
        });
    },
    //添加好友发送过来的信息
    [USERCLIENT.USER_MSG_LIST](state, data) {
        //判断发送过来的信息的人是否是自己
        //如果是自己的话就给好友房间添加信息 否则就相反
        if (state.userInfo.name === data.uid) {
            state.massageList[data.fid].push(data);
        } else {
            state.massageList[data.uid].push(data);
        }
    },
    /**
     * 发送信息
     * fid 接收人id
     * content 信息内容
     */
    [USERCLIENT.SET_USER_MSG_LIST](state, data) {
        state.userMsgList = {
            fid: data,
            content: state.massageList[data]
        };
    }
};
const userClient = {
    state,
    getters,
    actions,
    mutations
};
export default userClient;
